<template>
	<view class="house">
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="发布" :autoBack="true">
			</u-navbar>
		</view>
		<view class="con-body" style="padding-left: 20rpx; padding-right: 20rpx; background-color: #F8F8F8;">
			<!-- 发布 -->
			<view class="uni-flex-center">
				<view class="main-service uni-cell-90">
					<view class="user-list">
						<view style="padding: 20rpx 30rpx;">

							<u-textarea v-model="content" :border="false" placeholder="分享我的体验"></u-textarea>
							<view style="margin-top: 10rpx;">
								<text style="color: #007AFF;">{{topicTitle}}</text>
							</view>
							<u-line></u-line>
						</view>
						<view style="margin-top: 20rpx;margin-left: 40rpx;">
							<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
								:maxCount="9"></u-upload>
						</view>
						<view class="user-list" style="margin-top: 80rpx;">
							<view style="padding: 15rpx 20rpx; text-align: center; font-size: 24rpx; float: left; height: 40rpx;" v-for="(item,index) in themesList" v-if="index<5">
								<view v-if="current == item.id" class="btn-view" @click="topic(item.id, item.name)">
									<text># {{item.name}} #</text>
								</view>
								<view v-else class="btn-view1" @click="topic(item.id, item.name)">
									<text># {{item.name}} #</text>
								</view>
							</view>
							<view style="padding: 15rpx 20rpx; text-align: center; font-size: 24rpx; float: left; height: 40rpx;">
								<view class="btn-view1" @click="showpopup=true">
									<text># 更多 #</text>
								</view>
							</view>
							<view style="clear: both; height: 80rpx;"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-popup mode="bottom" :show="showpopup" :closeable="true" @close="showpopup=false">
			<view class="user-list" style="text-align: center;">
				<view style="padding:20rpx 0;font-size: 32rpx;font-weight: bold;">选择话题</view>
				<view style="padding: 15rpx 20rpx; text-align: center; font-size: 24rpx; display: inline-block; height: 40rpx;" v-for="(item,index) in themesList">
					<view v-if="current == item.id" class="btn-view" @click="topic(item.id, item.name)">
						<text># {{item.name}} #</text>
					</view>
					<view v-else class="btn-view1" @click="topic(item.id, item.name)">
						<text># {{item.name}} #</text>
					</view>
				</view>
				<view style="clear: both; height: 80rpx;"></view>
			</view>
		</u-popup>
		<u-button :customStyle="btnStyle" @click="release">发布</u-button>
	</view>
</template>

<script>
	import {
		getTopic,
		release
	} from "@/service/communitService.js";

	export default {
		data() {
			return {
				btnStyle: {
					width: '96%',
					borderRadius: '40rpx',
					marginLeft: '2%',
					backgroundImage: "url('https://s4.ax1x.com/2022/02/22/HxzlJf.png')",
					backgroundSize: '100% 80rpx',
					color: '#FFFFFF',

					position: 'fixed',
					bottom: '40rpx'
				},
				topicTitle: '',
				content: '',
				current: 0,
				fileList1: [],
				images: [],
				themesList: [],
				page: 1,
				limit: 3,
				showpopup:false
			}
		},
		onLoad() {
			getTopic({
				page: this.page,
				limit: 20
			}).then(this.getTopic);
		},
		methods: {
			getTopic(e) {
				if (e.code == 200) {
					this.themesList = e.result;
				}
			},
			topic(tid, name) {
				this.current = tid;
				this.topicTitle = '# ' + name + ' #';
				this.showpopup=false
			},
			release() {
				let par = {};

				par.content = this.content;
				par.images = this.images;
				par.topic = this.current;
				par.from = "龙庭社圈";
				par.location = "龙庭壹号院";


				release(par).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({
					
							type: 'success',
							title: '成功',
							message: res.msg,
							complete() {
								let pages = getCurrentPages();
								let beforePage = pages[pages.length - 2];
								beforePage.$vm.reFresh = Math.random()
								uni.navigateBack();
							}
						})
					} else {
						this.$refs.uToast.show({
					
							type: 'error',
							title: '失败',
							message: res.msg,
							complete() {
								
							}
						})
					}
				})

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.images.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://admin.gslhzhwy.com/api/upload/file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'smart-property'
						},
						success: (res) => {
							let par = JSON.parse(res.data);
							if (par.code == 1) {
								this.images.push(par.data.url);
							}
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	.btn-view {
		color: #FFFFFF;
		background-image: url("https://s4.ax1x.com/2022/02/22/HxzlJf.png");
		background-size: 100% 60rpx;
		border-radius: 30rpx;
		padding: 2rpx 15rpx;
	}

	.btn-view1 {
		color: #666666;
		border: #666666 1rpx solid;
		border-radius: 30rpx;
		padding: 2rpx 15rpx;
	}
</style>
